package com.fcscs.ecms.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fcscs.core.dao.ConnectionManager;
import com.fcscs.core.domain.BaseEntity.DBLoc;
import com.fcscs.cservice.domain.MainMenuItem;
import com.fcscs.cservice.ws.schema.common.MenuListView;
import com.fcscs.cservice.ws.schema.common.PictureView;
import com.fcscs.ecms.dao.ECMSHotelPerRegionDao;


@SuppressWarnings("unchecked")
@Repository("hotelPerRegionDao")
public class ECMSHotelPerRegionDaoHibernate implements ECMSHotelPerRegionDao {

	
	private static DBLoc TBLoc = new MainMenuItem().getDBLoc();

	@Autowired
	private ConnectionManager connMgr;
	
	
	@Override
	public List<MenuListView> getMenuListViews(long orgId, long propId, String langId, long regionId) {
	
		// TODO Auto-generated method stub
		String sql = 
				"SELECT ecms_hotel_staff_v02.global_hotel_entity.id," +
				"(select ecms_hotel_staff_v02.global_text.textUnicodeValue from ecms_hotel_staff_v02.global_text " +
					"where languageId = 'en_us' and id = ecms_hotel_staff_v02.global_hotel_entity.nameRef) as 'Title'," +
        "(select ecms_hotel_staff_v02.global_text.textUnicodeValue from ecms_hotel_staff_v02.global_text " +
          "where languageId ='en_us' and id = ecms_hotel_staff_v02.global_hotel_entity.descRef) as 'SubTitle', " +
          "ecms_hotel_staff_v02.global_hotel_entity.tabletPicRef, " +
        "(select ecms_hotel_staff_v02.global_picture.version from ecms_hotel_staff_v02.global_picture " +
					"where id = ecms_hotel_staff_v02.global_hotel_entity.tabletPicRef) as 'menuPicturerefVersion' " +
					"FROM ecms_hotel_staff_v02.mdl_our_hotels_main " +
						 "inner join ecms_hotel_staff_v02.global_region on global_region.id = ecms_hotel_staff_v02.mdl_our_hotels_main.regionId " +
						 "inner join ecms_hotel_staff_v02.global_hotel_entity on ecms_hotel_staff_v02.global_region.id = ecms_hotel_staff_v02.global_hotel_entity.regionId " +
						 "where ecms_hotel_staff_v02.mdl_our_hotels_main.proprietaryId = 1 and ecms_hotel_staff_v02.mdl_our_hotels_main.isActive = true " +
						"order by  ecms_hotel_staff_v02.mdl_our_hotels_main.displayOrder";
		Query sqlQu = connMgr.getCurrentSession(TBLoc).createSQLQuery(sql);
//		sqlQu.setParameter("orgId", orgId);
//		sqlQu.setParameter("propId", orgId);
//		sqlQu.setParameter("languageID", langId);
		

		List<MenuListView> lisMenuListViews = new ArrayList<MenuListView>();
		List<Object[]> objects = (List<Object[]>)sqlQu.list();
		for(Object[] objTmp : objects){
			MenuListView menuListView = new MenuListView();
			for(int i =0 ; i<objTmp.length ; i++){
				System.out.println(String.valueOf(objTmp[i]));
				
			}
			
			PictureView pictureView = new PictureView();
			pictureView.setId(Long.parseLong(objTmp[3].toString()));
			pictureView.setVersion(Long.parseLong(objTmp[4].toString()));
			
			
			menuListView.setId(Long.parseLong(String.valueOf(objTmp[0])));
			menuListView.setSubtitle(String.valueOf(objTmp[2]));
			menuListView.setTitle(String.valueOf(objTmp[1]));
			menuListView.setPictureView(pictureView);
			menuListView.setTypeId("1");
			lisMenuListViews.add(menuListView);			
		}
		
		return lisMenuListViews;
	}



}
